import React from 'react';
import { context } from './config';

class Son extends React.PureComponent {
  state = {
    num: 0,
  };

  static contextType = context;

  componentDidUpdate(props, state) {
    console.log(props, state, 'DidUpdate');
  }
  componentDidMount() {
    console.log('Son');
  }

  getSnapshotBeforeUpdate() {
    console.log('getSnapshotBeforeUpdate');
    return 'getSnapshotBeforeUpdate';
  }

  static getDerivedStateFromProps(props, state) {
    // 当组件挂载后执行一次
    // 当组件重新渲染时执行一次
    console.log(props, state, 'getDerivedStateFromProps');
    // 表示不需要更新
    return null;
  }

  render() {
    console.log(123123);
    return (
      <div>
        Son
        <button
          id="btn_son"
          onClick={() =>
            this.setState({
              num: this.state.num + 1,
            })
          }
        >
          {this.state.num}
        </button>
      </div>
    );
  }
}

export default Son;
